package de.doegel.common;

import greendroid.graphics.drawable.ActionBarDrawable;
import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import de.doegel.common.ormlite.OrmLiteGDActivity;
import de.doegel.common.util.Utils;

public class MainActivity<H extends AbstractDatabaseHelper> extends OrmLiteGDActivity<H> {
	protected ImageView titleImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.main);

		titleImageView = (ImageView) findViewById(R.id.gd_action_bar_home_item);

		((Button) findViewById(R.id.home_btn_news)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					startActivity(Utils.createNewsIntent(MainActivity.this));
				}
			}
		});
		((Button) findViewById(R.id.home_btn_messages)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					startActivity(Utils.createMessagesIntent(MainActivity.this));
				}
			}
		});
		((Button) findViewById(R.id.home_btn_contacts)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					startActivity(Utils.createContactsIntent(MainActivity.this));
				}
			}
		});
		((Button) findViewById(R.id.home_btn_search_contacts)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					Intent intent = Utils.createContactSearchIntent(MainActivity.this);
					intent.putExtra(IDoegelConstants.LAUNCHED_FROM_MAIN_ACTIVITY_EXTRA, true);
					startActivity(intent);
				}
			}
		});
		((Button) findViewById(R.id.home_btn_favorites)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					Intent intent = Utils.createAnimalHistoryAndFavoritesIntent(MainActivity.this);
					intent.putExtra(IDoegelConstants.IS_FAVORITES_EXTRA, true);
					startActivity(intent);
				}
			}
		});
		((Button) findViewById(R.id.home_btn_history)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Utils.getUserRole(MainActivity.this) < 1) {
					startActivity(Utils.createSettingsIntent(MainActivity.this));
				} else {
					Intent intent = Utils.createAnimalHistoryAndFavoritesIntent(MainActivity.this);
					intent.putExtra(IDoegelConstants.IS_FAVORITES_EXTRA, false);
					startActivity(intent);
				}
			}
		});
		((Button) findViewById(R.id.home_btn_search_animals)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = Utils.createAnimalSearchIntent(MainActivity.this);
				intent.putExtra(IDoegelConstants.LAUNCHED_FROM_MAIN_ACTIVITY_EXTRA, true);
				startActivity(intent);
			}
		});
		((Button) findViewById(R.id.home_btn_settings)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(Utils.createSettingsIntent(MainActivity.this));
			}
		});

		addActionBarItem(getActionBar().newActionBarItem(NormalActionBarItem.class).setDrawable(new ActionBarDrawable(this, R.drawable.ic_action_bar_info)), R.id.action_bar_view_info);
	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		if (item.getItemId() == R.id.action_bar_view_info) {
			startActivity(Utils.createInfoIntent(this));
			return true;
		} else {
			return super.onHandleActionBarItemClick(item, position);
		}
	}

	private static final int CONTEXT_MENU_SETTINGS = 0;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, CONTEXT_MENU_SETTINGS, 0, R.string.settings_context_menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case CONTEXT_MENU_SETTINGS:
			startActivity(Utils.createSettingsIntent(this));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
